//Script by Nitin Maheta of www.gj37765.blogspot.com
//Released on August 21th Septembar 2011
var relatedTitles = new Array();
var relatedTitlesNum = new Array();
var relatedUrls = new Array();
var thumburl = new Array();
var moreUrls = new Array();

function related_results_labels_thumbs(json,type) {
	if(type == "more")
	{
		relatedTitles = new Array();
		relatedUrls = new Array();
		thumburl = new Array();
		relatedTitlesNum = 0;
	}
	
    for (var i = 0; i < json.feed.entry.length; i++) {
        var entry = json.feed.entry[i];
        relatedTitles[relatedTitlesNum] = entry.title.$t;
        try {
            thumburl[relatedTitlesNum] = entry.media$thumbnail.url;
        } catch (error) {
            s = entry.content.$t;
            a = s.indexOf("<img");
            b = s.indexOf("src=\"", a);
            c = s.indexOf("\"", b + 5);
            d = s.substr(b + 5, c - b - 5);
            if ((a != -1) && (b != -1) && (c != -1) && (d != "")) {
                thumburl[relatedTitlesNum] = d;
            } else {
                if (typeof (defaultnoimage) !== 'undefined') thumburl[relatedTitlesNum] = defaultnoimage;
                else thumburl[relatedTitlesNum] = "http://1.bp.blogspot.com/-FeKY1cIGlbM/TnnKJ7XLnsI/AAAAAAAABY8/FJanm5jFIeU/s1600/noimage.png";
            }
        }
        //if (relatedTitles[relatedTitlesNum].length > 35) relatedTitles[relatedTitlesNum] = relatedTitles[relatedTitlesNum].substring(0, 35) + "...";
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                relatedUrls[relatedTitlesNum] = entry.link[k].href;
                relatedTitlesNum++;
            }
        }
    }
}
function removeRelatedDuplicates_thumbs() {
    var tmp = new Array(0);
    var tmp2 = new Array(0);
    var tmp3 = new Array(0);
    for (var i = 0; i < relatedUrls.length; i++) {
        if (!contains_thumbs(tmp, relatedUrls[i])) {
            tmp.length += 1;
            tmp[tmp.length - 1] = relatedUrls[i];
            tmp2.length += 1;
            tmp3.length += 1;
            tmp2[tmp2.length - 1] = relatedTitles[i];
            tmp3[tmp3.length - 1] = thumburl[i];
        }
    }
    relatedTitles = tmp2;
    relatedUrls = tmp;
    thumburl = tmp3;
}
function contains_thumbs(a, e) {
    for (var j = 0; j < a.length; j++)
        if (a[j] == e) return true;
    return false;
}
function printRelatedLabels_thumbs(current, _name, type, thumbwidth) {
    var splitbarcolor;
    if (typeof (splittercolor) !== 'undefined') splitbarcolor = splittercolor;
    else splitbarcolor = "#d4eaf2";
    for (var i = 0; i < relatedUrls.length; i++) {
        if ((current.indexOf(relatedUrls[i]) != -1) || (!relatedTitles[i])) {
            relatedUrls.splice(i, 1);
            relatedTitles.splice(i, 1);
            thumburl.splice(i, 1);
            i--;
        }
    }
    var r = Math.floor((relatedTitles.length - 1) * Math.random());
    var i = 0;
    if (relatedTitles.length > 0)
	{
		if(type == "related")
		{
			document.write('<h2>' + relatedpoststitle + '</h2>');
			document.write('<div style="clear: both;"></div>');
			while (i < relatedTitles.length && i < 20 && i < maxresults) {
				document.write('<a class="related_block" style="text-decoration:none;padding:5px;float:left;');
				if (i != 0) document.write('border-left:solid 0.5px ' + splitbarcolor + ';"');
				else document.write('"');
				document.write(' href="' + relatedUrls[r].replace(originalHostname, currentHostname) + '"><img class="related_img" src="' + thumburl[r].replace(/\/s72\-c/, "/s" + thumbwidth + "-c") + '"/><br/><div class="related_element">' + relatedTitles[r] + '</div></a>');		
				i++;
				
				moreUrls.push(relatedUrls[r]);
				
				if (r < relatedTitles.length - 1) {
					r++;
				} else {
					r = 0;
				}
			}
			//document.write('</div>');
		}
		else
		{
			var div_more = jQuery("#listMorePostLabel");
			var str_append = '<div style="clear: both;"></div><div class="morePostLabel"><h2><a href="http://'+currentHostname+'/search/label/'+_name+'" target="_blank">' + morepoststitle.concat(_name.toUpperCase()) + '</a></h2>';
			while (i < relatedTitles.length && i < 20 && i < maxresults) {
				str_append += '<a class="related_block" style="text-decoration:none;padding:5px;float:left;';
				if (i != 0) str_append += 'border-left:solid 0.5px ' + splitbarcolor + ';"';
				else str_append += ';"';
				str_append += ' href="' + relatedUrls[r].replace(originalHostname, currentHostname) + '"><img class="related_img" src="' + thumburl[r].replace(/\/s72\-c/, "/s" + thumbwidth + "-c") + '"/><br/><div class="related_element">' + relatedTitles[r] + '</div></a>';
				i++;
				
				moreUrls.push(relatedUrls[r]);
				
				if (r < relatedTitles.length - 1) {
					r++;
				} else {
					r = 0;
				}
			}
			str_append += '</div>';
			div_more.append(str_append);
		}
		/*relatedUrls.splice(0, relatedUrls.length);
		thumburl.splice(0, thumburl.length);
		relatedTitles.splice(0, relatedTitles.length);*/
	}
}

function change_name(str)
{
	str = decodeURIComponent(str);
	str = str.replace("+"," ");
	return str;
}
